<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include '../../include/dbFunctions.php';
include("../../include/class.phpmailer.php");
include ("../../include/image_slider.php");
include ("../../include/function.php");

$dbhost = "localhost";
$dbname = "s91199";
$dbuser = "s91199";
$dbpass = "123456";
$conn=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$role=mysqli_real_escape_string($conn,$_POST['role']);
if ($role=="school") {
    $sql = "SELECT * FROM contact_school cs WHERE cs.contact_email='$email'";
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    $count = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    $school_id=$row['school_id'];
    if ($count==1) {
        $randomPassword=md5(uniqid(rand()));
        $newPassword=substr($randomPassword, 0, 8);
        $updatePassword = SHA1($newPassword);
        $to = $_POST['email'];
        $subject = "JA Reset Password";
        $message = <<<HERE
This is Junior Achievement Singapore.

We have reset you Password and this is the new password: $newPassword

Please try to re-login using your new password at http://scheduler.ja.org.sg
Please be free to contact us for any enquiries.

Thank you for your support of JA Programs.
Best regards,
Ng. Hau Yee (Ms.)
Director
Junior Achievement Singapore


* It’s an auto-generated email. Please do not reply
Any question, please email hauyee.ng@ja.org
HERE;
        $headers = "From: hauyee.ng@ja.org" . "\r\n" .
                "Reply-To: hauyee.ng@ja.org" . "\r\n" .
                "X-Mailer: PHP/" . phpversion();

        $sendresult = mail($to, $subject, $message, $headers);
        if ($sendresult) {
            $sql = "UPDATE contact_school
                            SET contact_password ='".$newPassword."'
                            WHERE school_id='".$school_id."'";
            $result2 = mysqli_query($conn, $sql) or die(mysqli_error($conn));
            if($result2) {
                $message = "An email has been sent to" . $to . " <br/>Please check your email.<br>";
                $message.="<input type='submit' class ='form-back' value='Back' onclick='history.go(-1)'/>";
            }else{
                $message = "Error. Please try again";
                $message.="<input type='submit' class ='form-back' value='Back' onclick='history.go(-1)'/>";
            }

        }else{
                $message = "Error. Please try again";
                $message.="<input type='submit' class ='form-back' value='Back' onclick='history.go(-1)'/>";
            }
    }
}if ($role=="volunteer") {
    $sql = "SELECT * FROM volunteer v WHERE v.volunteer_email='$email'";
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    $count = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    $volunteer_id=$row['volunteer_id'];
    if ($count==1) {
        $randomPassword=md5(uniqid(rand()));
        $newPassword=substr($randomPassword, 0, 8);
        $updatePassword = SHA1($newPassword);
        $to = $_POST['email'];
        $subject = "JA Reset Password";
        $message = <<<HERE
This is Junior Achievement Singapore.

We have reset you Password and this is the new password: $newPassword

Please try to re-login using your new password at http://scheduler.ja.org.sg
Please be free to contact us for any enquiries.

Thank you for your support of JA Programs.
Best regards,
Ng. Hau Yee (Ms.)
Director
Junior Achievement Singapore


* It’s an auto-generated email. Please do not reply
Any question, please email hauyee.ng@ja.org
HERE;
        $headers = "From: hauyee.ng@ja.org" . "\r\n" .
                "Reply-To: hauyee.ng@ja.org" . "\r\n" .
                "X-Mailer: PHP/" . phpversion();

        $sendresult = mail($to, $subject, $message, $headers);
        if ($sendresult) {
            $sql = "UPDATE volunteer
                            SET volunteer_password ='".$updatePassword."'
                            WHERE volunteer_id='".$volunteer_id."'";
            $result2 = mysqli_query($conn, $sql) or die(mysqli_error($conn));
            if($result2) {
                $message = "An email has been sent to " . $to . " <br/>Please check your email. Thank you.<br>";
                $message.="<input type='submit' class ='form-back' value='Back' onclick='history.go(-1)'/>";
            }else{
                $message = "Error. Please try again";
                $message.="<input type='submit' class ='form-back' value='Back' onclick='history.go(-1)'/>";
            }

        }else{
                $message = "Error. Please try again";
                $message.="<input type='submit' class ='form-back' value='Back' onclick='history.go(-1)'/>";
            }
    }
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Junior Achievement Singapore</title>
        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery-1.6.1.min.js"></script>
        <link href="../../media/css/styles.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../../media/js/jquery/image_slider.js"></script>
        <!-- Custom jquery scripts -->
        <script src="../../media/js/jquery/custom_jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <script>
            var timeout    = 700;
            var closetimer = 0;
            var ddmenuitem = 0;

            function jsddm_open()
            {  jsddm_canceltimer();
                jsddm_close();
                ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

            function jsddm_close()
            {  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

            function jsddm_timer()
            {  closetimer = window.setTimeout(jsddm_close, timeout);}

            function jsddm_canceltimer()
            {  if(closetimer)
                {  window.clearTimeout(closetimer);
                    closetimer = null;}}

            $(document).ready(function()
            {  $('#navigation > li').bind('mouseover', jsddm_open)
                $('#navigation > li').bind('mouseout',  jsddm_timer)});

            document.onclick = jsddm_close;

        </script>
        <div id="container"><!--CONTAINER STARTS-->
            <div id="navigation">
                <li><a href="../../include/home/homepage.php">HOME</a></li>
                <li><a href="../../include/home/contact.php">CONTACT</a></li>
                <li><a href="http://ja.org.sg/">ABOUT JA SINGAPORE</a></li>
                <li><a href="http://jascheduling.lefora.com/">FEEDBACK</a></li>
                <li><a href="../../guide/guide_menu.php">HELP</a></li>

            </div>
            <div id="title"><!--TITLE STARTS-->
            </div><!--TITLE ENDS-->

            <div id="featured"><!--FEATURED AREA STARTS-->

               <div class="featured-image"><!--FEATURED IMAGE STARTS-->
                    <img alt="Featured Image" src="../../media/images/featured_image.png"/>
                </div><!--FEATURED IMAGE ENDS-->
                <div class="featured-text"><!--FEATURED TEXT STARTS-->

                    <h2>Welcome to JA Singapore</h2>
                    <p>Junior Achievement programs help prepare young people for the real world by showing them how to generate wealth and effectively manage it, how to create jobs which make their communities more robust, and how to apply entrepreneurial thinking to the workplace. <a href="http://jaworldwide.org">Read More... </a></p>

                </div><!--FEATURED TEXT ENDS-->

            </div><!--FEATURED AREA ENDS-->

            <?php
            custom_login();
            ?>
            <div id="left-content"><!--RIGHT CONTENT STARTS-->

                <div class="left-top"><!--RIGHT CONTENT BOX TOP-->
                </div><!--RIGHT CONTENT BOX TOP ENDS-->

                <div class="left-middle"><!--RIGHT CONTENT BOX MIDDLE-->

                    <?php
                    echo "<br/><br/><br/><br/><br/><center><p class='pmain'>$message</p></center><br/><br/><br/><br/><br/><br/><br/><br/>";
                    ?>

                </div><!--RIGHT CONTENT BOX MIDDLE ENDS-->

                <div class="left-bottom"><!--RIGHT CONTENT BOX BOTTOM-->
                </div><!--RIGHT CONTENT BOX BOTTOM ENDS-->
            </div><!--RIGHT CONTENT ENDS-->
        </div><!--CONTAINER ENDS-->
        <div id="footer"><!--FOOTER STARTS-->
            <p>Copyright &copy; Junior Achievement Singapore | All Rights Reserved</p>
        </div><!--FOOTER ENDS-->
    </body>
</html>